/*
 * @Author: liusheng
 * @Date: 2022-04-13 18:58:33
 * @LastEditors: liusheng
 * @LastEditTime: 2022-04-13 20:34:50
 * @Description: 剑指 Offer II 028. 展平多级双向链表
 * email:liusheng613@126.com
 * Copyright (c) 2022 by liusheng/liusheng, All Rights Reserved. 
 
剑指 Offer II 028. 展平多级双向链表
多级双向链表中，除了指向下一个节点和前一个节点指针之外，它还有一个子链表指针，可能指向单独的双向链表。这些子列表也可能会有一个或多个自己的子项，依此类推，生成多级数据结构，如下面的示例所示。

给定位于列表第一级的头节点，请扁平化列表，即将这样的多级双向链表展平成普通的双向链表，使所有结点出现在单级双链表中。

 

示例 1：

输入：head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
输出：[1,2,3,7,8,11,12,9,10,4,5,6]
解释：

输入的多级列表如下图所示：



扁平化后的链表如下图：


示例 2：

输入：head = [1,2,null,3]
输出：[1,3,2]
解释：

输入的多级列表如下图所示：

  1---2---NULL
  |
  3---NULL
示例 3：

输入：head = []
输出：[]
 

如何表示测试用例中的多级链表？

以 示例 1 为例：

 1---2---3---4---5---6--NULL
         |
         7---8---9---10--NULL
             |
             11--12--NULL
序列化其中的每一级之后：

[1,2,3,4,5,6,null]
[7,8,9,10,null]
[11,12,null]
为了将每一级都序列化到一起，我们需要每一级中添加值为 null 的元素，以表示没有节点连接到上一级的上级节点。

[1,2,3,4,5,6,null]
[null,null,7,8,9,10,null]
[null,11,12,null]
合并所有序列化结果，并去除末尾的 null 。

[1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
 

提示：

节点数目不超过 1000
1 <= Node.val <= 10^5
 

注意：本题与主站 430 题相同： https://leetcode-cn.com/problems/flatten-a-multilevel-doubly-linked-list/
 */

#include <functional>
using namespace std;

class Node {
public:
    int val;
    Node* prev;
    Node* next;
    Node* child;
};

//dfs solution
//similar to tree's post order traverse
class Solution {
public:
    Node* flatten(Node* head) {
        // & capture is needed,else the dfs call in the lambda will be error
        function<Node *(Node *)> dfs = [&](Node * head) {
        Node * cur = head;
        Node * pre = nullptr;
        while (cur)
        {
            pre = cur;
            // printf("while p p val:%d\n",p->val);
            Node * pNext = cur->next;
            if (cur->child)
            {
                Node * pChild_last = dfs(cur->child);

                // printf("cur val:%d,childlast val:%d\n",cur->val,pChild_last->val);

                cur->next = cur->child;
                cur->child->prev = cur;
                cur->child = nullptr;

                
                if (pNext)
                {
                    pChild_last->next = pNext;
                    pNext->prev = pChild_last;
                }

                //pre now point to the children's last element
                //when child was at last of one level,no this will be make an error
                pre = pChild_last;
            }
            cur = pNext;  
        }
        // printf("dfs pre:%d\n",pre->val);
        return pre;
        };

        dfs(head);
        return head;
    }
};

//flat node level by level
class Solution2 {
public:
    Node* flatten(Node* head) {
        Node * node = head;
        
        while (node)
        {
            //if node has child,connet node to the next level child
            //then just traverse node by step 1
            //every time flat one level,then the node list will be flated level by level
            if (node->child)
            {
                Node * nextNode = node->next;

                node->next = node->child;
                node->child->prev = node;
                node->child = nullptr;

                //node->next now is point to node's child
                //node->child has been set to nullptr
                Node * child_last = node->next;
                while (child_last->next)
                {
                    child_last = child_last->next;
                }

                if (nextNode)
                {
                    child_last->next = nextNode;
                    nextNode->prev = child_last;
                }
            }

            node = node->next;
        }

        return head;
    }
};